<?php

namespace app\service;


use app\model\Agent;
use app\model\CarQrcode;
use library\wechat;

class CarQrcodeService extends Base
{

    //创建小程序码数据
    public function create_qrcode_data()
    {

        $data = [
            'user_id' => null,
            'status' => 0
        ];

        $res = CarQrcode::create($data);

        return apiSuccess($res);
    }


    //创建小程序码
    public function create_qrcode($id)
    {
        $data = CarQrcode::where('id', $id)->find();

        if (!$data) {
            return apiError('数据不存在');
        }

        if ($data->status != 0) {
            return apiError('二维码生成');
        }
        $access_token = wechat::get_access_token();

        $res = wechat::get_wxa_code($access_token, $id,'qrcode_id', 'wxa_2', true);
        $data->status = 1;
        $data->qrcode = $res;
        $data->save();
        return apiSuccess($data);
    }

    //创建代理小程序码
    public function create_agent_qrcode($id)
    {
        $data = Agent::where('id', $id)->find();
        if (!$data) {
            return apiError('数据不存在');
        }
        $access_token = wechat::get_access_token();
        $res = wechat::get_wxa_code($access_token, $id,'agent_id', 'wxa_agent', true,'pages/yd_home/home');
        $data->img = $res;
        $data->save();
        return apiSuccess($res);
    }



}